/*
 * @Description: 调试api仓库
 * @Autor: huoyou
 * @Date: 2022-01-11 11:10:39
 * @LastEditTime: 2022-04-21 15:26:41
 */

const fs = require("fs");
const path = require("path");
const http = require("http");
const axios = require("axios");
function refresh(url, params, type) {
  axios.get(url, { params }).then((res) => {
    if (res.data.state) {
      var content;
      if (type === "ctmedia") {
        content = res.data.data.detail.content.content;
      } else {
        content = res.data.data.common.content;
      }
      generateFile(content);
    } else {
      console.error("报错了~");
    }
  });
}

function generateFile(data) {
  let file = path.resolve(__dirname, "../index.html");
  fs.writeFile(file, data, "utf8", (err) => {
    if (err) {
      console.warn(err.message);
    } else {
      console.log("生成成功");
    }
  });
}

module.exports = function (...args) {
  var baseUrl = {
    dev: "https://m-api.dutetest.com",
    pre: "https://m-api.szdute.cn",
    pro: "https://m-api.dutenews.com",
  };
  console.log(`🚀 ~ args`, args[0], args[1], args[2]); // 环境  类型  id
  if (args[1] === "audio") { 
    baseUrl = {
      dev: "https://cdn-m-api.dutetest.com",
      pre: "https://cdn-m-api.szdute.cn",
      pro: "https://cdn-m-api.dutenews.com",
    };
  }
  var params = {
    appid: "1",
    siteid: 10001,
    sharesiteid: 10001,
    clientid: 1,
    modules: "common:2",
    areas: ",,",
    type: "android",
    contentid: args[2],
    cache: '0'
  };
  if (args[1] === "ctmedia") {
    params = {
      app_version: "6.1.1",
      clientid: 1,
      contentid: args[2],
      device_id: "2E3250EC-7D0D-4BBE-99C4-1575D980767A",
      ip: "172.20.207.28",
      memberid: 641780,
      modules: "detail:1",
      sign: "4d3415e0634d75253f73e148a571d576",
      siteid: 10001,
      system_name: "6.1.1",
      time: 1619488978000,
      type: "android",
      cache: '0'
    };
  }
  var url = `${baseUrl[args[0]]}/v2/${args[1]}`;
  console.log(`🚀 ~ url`, url);
  refresh(url, params, args[1]);
  // return axios.get(url, { params });
  // axios.get(url, { params }).then((res) => {
  //   if (res.data.state) {
  //     content = res.data.data.common.content;
  //     return content;
  //   }
  // });
};
